package com.wwj.controller.admin;

import com.wwj.common.result.PageResult;
import com.wwj.common.result.Result;
import com.wwj.pojo.dto.SetmealDTO;
import com.wwj.pojo.dto.SetmealPageQueryDTO;
import com.wwj.pojo.entity.Check;
import com.wwj.pojo.vo.SetmealVO;
import com.wwj.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController("adminSetmealController")
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags="套餐相关接口")
public class SetmealController {
    @Resource
    private SetmealService setmealService;


    @PostMapping
    @ApiOperation(value = "新增套餐")
    //清除缓存注解
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result save(@RequestBody SetmealDTO setmealDTO){
        setmealService.save(setmealDTO);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation(value = "删除套餐")
    //清除缓存注解
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result delete(Long id){
        setmealService.deleteByIds(id);
        return Result.success();
    }

    @PutMapping
    @ApiOperation(value = "修改套餐")
    //清除缓存注解
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        setmealService.update(setmealDTO);
        return Result.success();
    }


    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult pageResult=setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据套餐id查询检查项目")
    public Result<List<Check>> selectSetmealWithDishById(@PathVariable Long id){
        List<Check> checks=setmealService.selectCheckBySetmealId(id);
        return Result.success(checks);
    }

    @PostMapping("/status/{status}")
    @ApiOperation(value = "套餐起售、停售")
    //清除缓存注解
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result status(@PathVariable Integer status,Long id){
        setmealService.status(status,id);
        return Result.success();
    }
}
